﻿#include "FITKAbaqusHDF5AdaptorAbaWire.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"
#include "FITK_Interface/FITKInterfaceModel/FITKCoordinateSystem.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaWire.h"

namespace IO
{
    QString FITKAbaqusHDF5AdaptorAbaWire::getAdaptorClass()
    {
        return "FITKAbaqusHDF5AdaptorAbaWire";
    }

    bool FITKAbaqusHDF5AdaptorAbaWire::adaptR()
    {
        auto wire = dynamic_cast<Interface::FITKAbaWire*>(_dataObj);
        if (!_reader || !wire || !_h5Group) return false;
        bool isR = true;
        isR &= readAbaWire(wire, *_h5Group);
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorAbaWire::adaptW()
    {
        auto wire = dynamic_cast<Interface::FITKAbaWire*>(_dataObj);
        if (!_writer || !wire || !_h5Group) return false;
        bool isW = true;
        isW &= writeAbaWire(wire, *_h5Group);
        return isW;
    }

    bool FITKAbaqusHDF5AdaptorAbaWire::readAbaWire(Interface::FITKAbaWire * wire, H5::Group & h5Group)
    {
        if (!wire) return false;
        bool isR = true;
        bool isValid = false;
        if (!readNDataObject(wire, h5Group)) return false;
        int count = readIntAttribute(h5Group, "DataCount");
        for (int i = 0; i < count; ++i)
        {
            auto data = wire->getDataByIndex(i);
            //异常处理
            if (!h5Group.nameExists(std::to_string(i))) continue;
            auto dataGroup = h5Group.openGroup(std::to_string(i));

            auto sAssElementType = readStrAttribute(dataGroup, "AssElementType");
            Core::FITKEnumTransfer<Interface::FITKAbstractAbaAssemblyElement::FITKAssElementType> fitkTypeTrafer;
            auto eAssElementType = fitkTypeTrafer.fromString(QString::fromStdString(sAssElementType), isValid);
            if (!isValid) return false;
            Interface::FITKAbaAssEle3D2* elementLine{};

            if (eAssElementType == Interface::FITKAbstractAbaAssemblyElement::FITKAssElementType::CONN3D2)
            {
                elementLine = new Interface::FITKAbaAssEleCONN3D2;
            }
            else if (eAssElementType == Interface::FITKAbstractAbaAssemblyElement::FITKAssElementType::SpringDashpotA)
            {
                elementLine = new Interface::FITKAbaAssEleSpringDashpot;

            }
            if (!elementLine) return false;

            wire->appendDataObj(elementLine);
            bool r = FITKInterfaceHDF5AdaptorAssemblyElementLine::readAssemblyElementLine(elementLine, dataGroup);
            if (!r)delete elementLine;
            isR &= r;
        }

        return isR;
    }

    bool FITKAbaqusHDF5AdaptorAbaWire::writeAbaWire(Interface::FITKAbaWire * wire, H5::Group & h5Group)
    {
        if (!wire) return false;
        bool isW = true;
        bool isValid = false;

        std::string groupName = createParentAttribute(wire, h5Group);
        if (groupName.empty()) return false;
        _h5CreateGroup = h5Group.createGroup(groupName);
        if (!writeNDataObject(wire, _h5CreateGroup)) return false;

        int count = wire->getDataCount();
        writeIntAttribute(_h5CreateGroup, "DataCount", &count);
        for (int i = 0; i < count; ++i)
        {
            auto data = wire->getDataByIndex(i);
            auto dataGroup = _h5CreateGroup.createGroup(std::to_string(i));

            auto eAssElementType = data->getAssElementType();
            Core::FITKEnumTransfer<Interface::FITKAbstractAbaAssemblyElement::FITKAssElementType> fitkTypeTrafer;
            auto sAssElementType = fitkTypeTrafer.toStrting(eAssElementType, isValid);
            if (!isValid) return false;
            writeStrAttribute(dataGroup, "AssElementType", sAssElementType.toStdString());
            isW &= FITKInterfaceHDF5AdaptorAssemblyElementLine::writeAssemblyElementLine(data, dataGroup);
        }
        return isW;
    }
}


